package com.cwt_taxi_xa.record;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.model.RecorderInfo;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.protocol.Encoder;
import com.cwt_taxi_xa.protocol.EncoderUtil;
import com.cwt_taxi_xa.timer.TimerHeartBeat;
import com.cwt_taxi_xa.util.AppLog;

import java.util.ArrayList;
import java.util.List;

/**
 * 录音上传管理者
 * @author wlh
 *
 */
public class RecorderUploadManager {

	public static final String TAG = "RecUploadManager";

	private static RecorderBiz recordingBiz;

	/**
	 * 是否正在上传
	 */
	private static boolean isWork = false;

	/**
	 * 需要排队
	 */
	private static List<RecorderInfo> infos = null;

	private static RecorderInfo info;

	/** 上传到第几包*/
	private static int index = -1;

	private static ArrayList<byte[]> listData;

	public static RecorderBiz getRecordingBiz() {
		if(recordingBiz == null){
			recordingBiz = new RecorderBiz(MainApplication.getInstance());
		}
		return recordingBiz;
	}

	/**
	 * 当前信息
	 */
	private static RecorderInfo getCurrentRec() {
		if(infos != null && infos.size() != 0){
			return infos.get(0);
		}
		return null;
	}

	/**
	 * 保存数据，需要上传时上传
	 */
	public static void uploadARecording(RecorderInfo info){
		if(infos == null){
			infos = new ArrayList<RecorderInfo>();
		}
		infos.add(info);
		AppLog.i(TAG, "添加上传音频；排队音频个数："+infos.size());
		if(!isWork){
			nextUpload();
		}
		TimerHeartBeat.uploadRecCount = 0;
		TimerHeartBeat.postRecording = 0;
	}

	/**
	 * 第一包
	 */
	private static void nextUpload(){
		if(infos != null && infos.size() != 0){
			info = infos.get(0);
			index = 0;
			isWork = true;
			listData = info.getListData();
			AppLog.i(TAG, "音频上传，第1包数据，共"+listData.size()+"包数据; 文件："+info.getTime()+"; 大小："+info.getSize());
			byte[] subData = listData.get(index);
			info.setFileHead(EncoderUtil.getRecordingFileHeadByte(info));
			DispatchCenterUsers.witeMsgConnected(Encoder.multiRecordUpload(info.getSerialId(),
					Integer.parseInt(info.getMediaId()),
					(int)info.getSize(), index, info.getFileHead(), subData));
			TimerHeartBeat.uploadRecCount = 0;
			TimerHeartBeat.postRecording = 0;
		}else{
			clearInfo();
		}
	}

	public static void clearInfo() {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "/////////////////////////////////////////清空数据");
		isWork = false;
		infos = null;
		info = null;
		listData = null;
		index = 0;
	}

	/**
	 * 重传
	 */
	public static void reProgress(){
		try {
			if(infos != null){
				AppLog.i(TAG, "音频上传，重传");
				if(--index == -1){
					nextUpload();
				}else{
					progress();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 滚动包
	 */
	public static void progress(){
		AppLog.i(TAG, "---------------------index:"+index+";listData.size():"+listData.size());
		if(++index >= listData.size()){
			doEnd();
		}else{
			TimerHeartBeat.uploadRecCount = 0;
			TimerHeartBeat.postRecording = 0;
			AppLog.i(TAG, "音频上传，第"+(index+1)+"包数据");
			isWork = true;
			byte[] subData = listData.get(index);
			DispatchCenterUsers.witeMsgConnected(Encoder.multiRecordUpload(info.getSerialId(),
					Integer.parseInt(info.getMediaId()),
					(int)info.getSize(), index, info.getFileHead(), subData));
		}
	}

	/**
	 * 上传结束
	 */
	public static void doEnd(){
		AppLog.i(TAG, "上传结束，index:"+index);
		infos.remove(0);
		nextUpload();
	}

	public static boolean isWork() {
		return isWork;
	}

	/**
	 * 清除上传程序
	 */
	/*public static void doClear(){

		index = -1;
		isWork = false;
		if(infos != null && infos.size() != 0){
			infos.clear();
			infos = null;
		}
	}*/

	/**
	 * 触发上传指令
	 */
	/*public static void upload(){
		try {
			NetWorkTimer.uploadPhotoCount = 0;
			isWork = true;
			RecorderInfo info = getCurrentRec();
			listData = info.getListData();
			byte[] subData = listData.get(index);
			DispatchCenterUsers.witeMsg(QHEncoder.multiRecordUpload(serial, mediaID,
					dataSize, loadIndex, subData), 0);
			MediaPostBiz.getInstance().postMediaData(QHEncoder.multiRecordUpload(info.getSerialId(),
					Integer.parseInt(info.getMediaId()),
					(int)info.getSize(), index, info.getFileHead(), subData));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	/**
	 * 数据补传
	 */
	/*public static void reload(final String mediaId, final int[] id){
		try {
			PhotoInfo info = getPhotoInfoBiz().getInfoByMediaId(mediaId);
			if(info != null){

				File file = new File(Constants.CAMERA_PATH, mediaId+".jpg");
				if(file.exists()){
					byte[] photo = FileOper.getBytesFromFile(file);
					byte[] locationInfo = Tools.parseHexStr2Byte(info.getLocationInfo());
					// 总包数
					int pkgCount;
					// 最后一包的长度
					int lastLen;
					if(photo.length % 512 == 0){
						pkgCount = photo.length / 512;
						lastLen = 512;
					}else{
						pkgCount = photo.length / 512 + 1;
						lastLen = photo.length % 512;
					}
					for(int i = 0;i<id.length;i++){
						// 第几包
						int pkgNum = id[i];
						// 第几包的长度
						int pkgLen = 512;
						// 照片数据的最后一包
						if(pkgNum == pkgCount){
							pkgLen = lastLen;
						}
						// 分包数据
						byte[] subData = new byte[pkgLen];
						System.arraycopy(photo, 512 * (pkgNum-1), subData, 0, pkgLen);
						AppLog.i(TAG, "多媒体ID:"+mediaId+"; 重传包总数:"+pkgCount+"; dataId:"+pkgNum);
						//						DispatchCenterUsers.witeMsg(QHEncoder.multimediaDataUpload(
						//								Integer.parseInt(info.getMediaId()), 0, 0, info.getEventCode(), info.getChannelId(), 
						//								subData,pkgCount, pkgNum, locationInfo), 0);
					}
				}
			}
			UploadManager.doEnd(mediaId);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
}
